package cn.tedu.csmall.sso.controller;

import cn.tedu.csmall.commons.pojo.admin.dto.AdminAddDTO;
import cn.tedu.csmall.commons.pojo.admin.dto.AdminLoginDTO;
import cn.tedu.csmall.commons.pojo.admin.vo.AdminListVO;
import cn.tedu.csmall.commons.restful.JsonResult;
import cn.tedu.csmall.sso.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.neo4j.Neo4jProperties;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admins")
@Api(tags = "管理员管理模块")
@Slf4j
public class AdminController {
    @Autowired
    private IAdminService adminService;



    @ApiOperation("添加管理员")
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody AdminAddDTO adminAddDTO){
        adminService.addNew(adminAddDTO);
        return JsonResult.ok("添加管理员成功");
    }

    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    //Security检查该用户是否有 /ams/admin/read 权限执行该方法
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListVO>> getListAdmins(){
        List<AdminListVO> adminListVOS = adminService.list();
        return JsonResult.ok("查询管理员列表成功",adminListVOS);
    }

    //初始化登录接口
    @ApiOperation("管理员登录")
    @PostMapping("/login")
    // 安全框架 登录请求 携带的参数
    // form格式===》数据自动映射 所以不需要加注解解析
    public JsonResult login(@RequestBody AdminLoginDTO adminLoginDTO){
        log.info("登录请求接受的参数:{}",adminLoginDTO);
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok("登录成功",jwt);
    }
}
